local skill = fk.CreateSkill{
  name = "tea__pianpian",
}
local U = require "packages/utility/utility"
local getNeededNumbers = function(room)
  local arr = {}
  for _, p in ipairs(room.alive_players) do
    for _, id in ipairs(p:getCardIds("ej")) do
      table.insertIfNeed(arr, Fk:getCardById(id).number)
    end
  end
  local numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
  numbers = table.filter(numbers, function(num)
    return not table.contains(arr, num)
  end)
  return numbers
end
---@type ViewAsSkillSpec
local pianpian = {
  nim_type = "special",
  pattern = ".|.|.|.|.|basic",
  mute = true,
  prompt = function()
    local laizi = table.concat(getNeededNumbers(Fk:currentRoom()), ",")
    return "#tea__pianpian-prompt:::" .. laizi
  end,
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames("tea__pianpian", all_names, player:getCardIds("h"))
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected == 0 then
      return true
    elseif #selected == 1 then
      local laizi = getNeededNumbers(Fk:currentRoom())
      if Fk:getCardById(selected[1]):getMark("@@juejue_mark-inhand") > 0 then
        if Fk:getCardById(to_select):getMark("@@juejue_mark-inhand") > 0 then
          return table.find(laizi, function(num)
            return table.contains(laizi, num + 1) or table.contains(laizi, num - 1)
          end)
        else
          local num = Fk:getCardById(to_select).number
          return table.contains(laizi, num + 1) or table.contains(laizi, num - 1)
        end
      else
        if Fk:getCardById(to_select):getMark("@@juejue_mark-inhand") > 0 then
          local num = Fk:getCardById(selected[1]).number
          return table.contains(laizi, num + 1) or table.contains(laizi, num - 1)
        else
          return math.abs(Fk:getCardById(to_select).number - Fk:getCardById(selected[1]).number) == 1
        end
      end
    else
      return false
    end
  end,
  view_as = function(self, player,  cards)
    if not self.interaction.data or #cards ~= 2 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local card1 = Fk:getCardById(use.card.subcards[1])
    local card2 = Fk:getCardById(use.card.subcards[2])
    local num1 = 0
    local num2 = 0
    room:notifySkillInvoked(player, self.name, "special")
    player:broadcastSkillInvoke(skill.name, table.random({1, 2}))
    local laizi = getNeededNumbers(room)
    if card1:getMark("@@juejue_mark-inhand") > 0 then
      if card2:getMark("@@juejue_mark-inhand") > 0 then
        if card1.number ~= 13 then
          num1 = card1.number
          num2 = card1.number + 1
        else
          num1 = 13
          num2 = 12
        end
      else
        num2 = card2.number
        local nums = table.filter(laizi, function(n)
          return n == card2.number + 1 or n == card2.number - 1
        end)
        if #nums > 0 then
          num1 = nums[1]
        else
          num1 = 0
        end
      end
    else
      if card2:getMark("@@juejue_mark-inhand") > 0 then
        num1 = card1.number
        local nums = table.filter(laizi, function(n)
          return n == card1.number + 1 or n == card1.number - 1
        end)
        if #nums > 1 then
          num2 = nums[2]
        elseif #nums == 1 then
          num2 = nums[1]
        else
          num2 = 0
        end
      else
        num1 = card1.number
        num2 = card2.number
      end
    end
      
    local name = use.card.trueName
    if #player:getTableMark("@tea__pianpian_record") > 0 then
      if name ~= player:getTableMark("@tea__pianpian_record")[3] then
        --player:broadcastSkillInvoke(skill.name, 3)
        player:drawCards(1, self.name)
      end
      local num3 = player:getTableMark("@tea__pianpian_record")[1]
      local num4 = player:getTableMark("@tea__pianpian_record")[2]

      if math.abs(num1 + num2 - num3 -num4) == 4 and player:isWounded() then
        --player:broadcastSkillInvoke(skill.name, 4)
        player.room:recover{
          who = player,
          num = 1,
          skillName = self.name
        }
      end
    end
    player.room:setPlayerMark(player, "@tea__pianpian_record", {num1, num2, name})
  end,
  enabled_at_play = function(self, player)
    return true
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
}
skill:addEffect("viewas", pianpian)
return skill